home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / Debugger / Java Debugger / JavaDebugger.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  12.0 KB  |  459 lines

  1. //
  2. // JavaDebugger.java
  3. //
  4. // (C) Copyright 1996 - 1998 Microsoft Corporation, All rights reserved.
  5. //
  6.  
  7.  
  8. import com.ms.com.*;
  9. import com.ms.debug.*;
  10. import com.ms.lang.*;
  11.  
  12.  
  13. public class JavaDebugger implements IRemoteDebugManagerCallback,
  14.                                      IRemoteProcessCallback
  15. {
  16.     /* Constants
  17.      ************/
  18.  
  19.     // Set a breakpoint at PC 0 in Hello.main().
  20.  
  21.     protected final String sDebugClass      = "Hello";
  22.     protected final String sDebugMethod     = "main";
  23.     protected final int cnBreakpointPC      = 0;
  24.  
  25.     // Debug registry key
  26.  
  27.     protected final String sJavaVMKey       = "Software\\Microsoft\\Java VM";
  28.     protected final String sDebug           = "Debug";
  29.  
  30.  
  31.     /* Fields
  32.     *********/
  33.  
  34.     protected IRemoteDebugManager m_irdm;
  35.     protected IRemoteProcess m_irp;
  36.     protected RegKey m_rkJavaVM;
  37.  
  38.  
  39.     /* Methods
  40.     **********/
  41.  
  42.     public static void main(String asCmdLine[])
  43.     {
  44.         JavaDebugger jd;
  45.  
  46.         jd = new JavaDebugger();
  47.  
  48.         jd.RunDebugger(asCmdLine);
  49.     }
  50.  
  51.     public synchronized void RunDebugger(String asCmdLine[])
  52.     {
  53.         // Exit on notify() or interrupt().
  54.  
  55.         try
  56.         {
  57.             RemoteDebugManager rdm = new RemoteDebugManager();
  58.  
  59.             System.out.println("Created RemoteDebugManager.");
  60.  
  61.             Initialize(rdm);
  62.  
  63.             System.out.println("Initialized debugger.");
  64.  
  65.             Run(asCmdLine);
  66.  
  67.             System.out.println("Running debuggee...");
  68.  
  69.             wait();
  70.         }
  71.         catch (InterruptedException ex)
  72.         {
  73.             ;
  74.         }
  75.         finally
  76.         {
  77.             Terminate();
  78.  
  79.             System.out.println("Terminated debugger.");
  80.         }
  81.     }
  82.  
  83.     public void Initialize(IRemoteDebugManager irdm)
  84.     {
  85.         m_irdm = irdm;
  86.  
  87.         m_irdm.RegisterCallback(this);
  88.  
  89.         System.out.println("Registered IRemoteDebugManagerCallback.");
  90.  
  91.         CreateDebugKey();
  92.     }
  93.  
  94.     public void Terminate()
  95.     {
  96.         DeleteDebugKey();
  97.  
  98.         if (m_irp != null)
  99.         {
  100.             m_irp.Detach();
  101.             m_irp = null;
  102.  
  103.             System.out.println("Detached IRemoteProcessCallback from IRemoteProcess.");
  104.         }
  105.  
  106.         if (m_irdm != null)
  107.         {
  108.             m_irdm.Detach();
  109.             m_irdm = null;
  110.  
  111.             System.out.println("Detached IRemoteDebugManagerCallback from IRemoteDebugManager.");
  112.         }
  113.     }
  114.  
  115.     public void CreateDebugKey()
  116.     {
  117.         try
  118.         {
  119.             RegKey rkDebug;
  120.  
  121.             m_rkJavaVM = new RegKey(RegKey.getRootKey(RegKey.LOCALMACHINE_ROOT), sJavaVMKey, RegKey.KEYOPEN_CREATE);
  122.             rkDebug = new RegKey(m_rkJavaVM, sDebug, RegKey.KEYOPEN_CREATE);
  123.         }
  124.         catch (RegKeyException rkex)
  125.         {
  126.             ;
  127.         }
  128.     }
  129.  
  130.     public void DeleteDebugKey()
  131.     {
  132.         try
  133.         {
  134.             m_rkJavaVM.deleteSubKey(sDebug);
  135.         }
  136.         catch (Exception ex)
  137.         {
  138.             ;
  139.         }
  140.     }
  141.  
  142.     public void Run(String asCmdLine[])
  143.     {
  144.         String sCmdLine;
  145.         int i;
  146.         DebuggeeProcess dp;
  147.         int nProcessID;
  148.  
  149.         sCmdLine = new String();
  150.  
  151.         for (i = 0; i < asCmdLine.length; i++)
  152.             sCmdLine += asCmdLine[i] + " ";
  153.  
  154.         System.out.println("Debuggee command line is \"" + sCmdLine + "\".");
  155.  
  156.         dp = new DebuggeeProcess();
  157.  
  158.         dp.CreateSuspendedProcess(sCmdLine);
  159.  
  160.         System.out.println("Created suspended debuggee process.");
  161.  
  162.         nProcessID = dp.GetProcessID();
  163.  
  164.         System.out.println("Debuggee process ID is " + nProcessID + ".");
  165.  
  166.         m_irdm.RequestCreateEvent(sDebugClass, nProcessID);
  167.  
  168.         System.out.println("Requested creation event on debuggee class " + sDebugClass + " in process " + nProcessID + ".");
  169.  
  170.         dp.ResumeProcess();
  171.  
  172.         System.out.println("Resumed debuggee process.");
  173.     }
  174.  
  175.     //
  176.     // Dumps the method bytecodes for the given method.  Demonstrates use of the
  177.     // LockBytesHelper class.
  178.     //
  179.     static void DumpMethodBytes(IRemoteMethodField irmf)
  180.     {
  181.         ILockBytes ilb;
  182.         byte[] abyteCode;
  183.         int i;
  184.  
  185.         ilb = irmf.GetBytes();
  186.  
  187.         abyteCode = LockBytesHelper.GetBytes(ilb);
  188.  
  189.         System.out.println(abyteCode.length + " method bytecodes:");
  190.  
  191.         for (i = 0; i < abyteCode.length; i++)
  192.             System.out.println("\tPC[" + i + "] = " + abyteCode[i]);
  193.     }
  194.  
  195.     //
  196.     // Dumps the line number information for the given method.
  197.     //
  198.     static void DumpMethodLineInfo(IRemoteMethodField irmf)
  199.     {
  200.         try
  201.         {
  202.             IJavaEnumLINEINFO ijeli;
  203.             LINEINFO li = new LINEINFO();
  204.  
  205.             ijeli = irmf.GetLineInfo();
  206.  
  207.             System.out.println("Method line number information:");
  208.  
  209.             while (true)
  210.             {
  211.                 try
  212.                 {
  213.                     ijeli.GetNext(li);
  214.  
  215.                     System.out.println("\tPC = " + li.offPC + ", line = " + li.iLine);
  216.                 }
  217.                 catch (ComSuccessException cse)
  218.                 {
  219.                     break;
  220.                 }
  221.             }
  222.         }
  223.         catch (ComFailException cfe)
  224.         {
  225.             System.out.println("No line information for " + irmf.GetName() + "().");
  226.         }
  227.     }
  228.  
  229.     //
  230.     // Dumps the bytes for the given constant pool item.  Demonstrates use of
  231.     // the ConstantPoolHelper class.
  232.     //
  233.     static void DumpConstantPoolItem(IRemoteClassField ircf, int niItem)
  234.     {
  235.         ConstantPoolHelper cph;
  236.         byte[] abyteItem;
  237.         int i;
  238.  
  239.         cph = new ConstantPoolHelper(ircf);
  240.  
  241.         abyteItem = cph.GetConstantPoolItem(niItem);
  242.  
  243.         System.out.println(abyteItem.length + " bytes for constant pool item " + niItem + ":");
  244.  
  245.         for (i = 0; i < abyteItem.length; i++)
  246.             System.out.println("\titem[" + i + "] = " + abyteItem[i]);
  247.     }
  248.  
  249.     //
  250.     // Debugger event notification methods return an HRESULT as an int as
  251.     // follows:
  252.     //
  253.     //      S_FALSE     Continue execution.
  254.     //
  255.     //      S_OK        Suspend execution of all threads in this namespace until
  256.     //                  an IRemoteThread method is called on this thread to
  257.     //                  resume execution.
  258.     //
  259.     //      E_...       Error.
  260.     //
  261.  
  262.     // IRemoteDebugManagerCallback methods
  263.  
  264.     public void ProcessCreateEvent(IRemoteProcess irpNew, IRemoteProcess irpParent)
  265.     {
  266.         // Register process callback.
  267.  
  268.         System.out.println("Received IRemoteProcessCallback::ProcessCreateEvent().");
  269.  
  270.         irpNew.RegisterCallback(this);
  271.         m_irp = irpNew;
  272.  
  273.         System.out.println("Registered IRemoteProcessCallback.");
  274.  
  275.         DeleteDebugKey();
  276.  
  277.         throw new ComSuccessException();
  278.     }
  279.  
  280.     // IRemoteProcessCallback methods
  281.  
  282.     public synchronized void ProcessDestroyEvent(IRemoteThread irth)
  283.     {
  284.         // Unregister process callback.
  285.  
  286.         System.out.println("Received IRemoteProcessCallback::ProcessDestroyEvent().");
  287.  
  288.         m_irp.Detach();
  289.         m_irp = null;
  290.  
  291.         System.out.println("Detached IRemoteProcessCallback from IRemoteProcess.");
  292.  
  293.         notify();
  294.  
  295.         throw new ComSuccessException();
  296.     }
  297.  
  298.     public void ClassLoadEvent(IRemoteThread irth, IRemoteClassField ircfClass)
  299.     {
  300.         String sClassName;
  301.  
  302.         System.out.println("Received IRemoteProcessCallback::ClassLoadEvent().");
  303.  
  304.         // Is this the class that a breakpoint is to be set on?
  305.  
  306.         sClassName = ((IRemoteField)ircfClass).GetName();
  307.  
  308.         if (sClassName.equals(sDebugClass))
  309.         {
  310.             IJavaEnumRemoteField ierf;
  311.             IRemoteField irf;
  312.  
  313.             System.out.println("Loaded class " + sClassName + " is being debugged.");
  314.  
  315.             // Yes.  Look for the method to set a breakpoint on.
  316.  
  317.             ierf = ((IRemoteContainerField)ircfClass).GetFields(FIELDKIND.FIELD_KIND_METHOD, 0, sDebugMethod);
  318.  
  319.             irf = ierf.GetNext();
  320.  
  321.             // Found the method.  Set a breakpoint.
  322.  
  323.             ((IRemoteMethodField)irf).SetBreakpoint(cnBreakpointPC);
  324.  
  325.             System.out.println("Set breakpoint on " + sDebugClass + "." + sDebugMethod + "()." + cnBreakpointPC + ".");
  326.         }
  327.  
  328.         throw new ComSuccessException();
  329.     }
  330.  
  331.     public void CodeBreakpointEvent(IRemoteThread irth)
  332.     {
  333.         IRemoteMethodField irmf;
  334.         IRemoteClassField ircf;
  335.  
  336.         // Clear the breakpoint.
  337.  
  338.         System.out.println("Received IRemoteProcessCallback::CodeBreakpointEvent().");
  339.  
  340.         System.out.println("Hit breakpoint at Hello.main().");
  341.  
  342.         // ((IRemoteMethodField)(irth.GetCurrentFrame().GetMethodObject().GetType())).ClearBreakpoint(cnBreakpointPC);
  343.  
  344.         irmf = (IRemoteMethodField)(irth.GetCurrentFrame().GetMethodObject().GetType());
  345.  
  346.         // Dump this method's bytecodes.
  347.  
  348.         DumpMethodBytes(irmf);
  349.  
  350.         // Dump this method's line number information.
  351.  
  352.         DumpMethodLineInfo(irmf);
  353.  
  354.         ircf = (IRemoteClassField)(irmf.GetContainer());
  355.  
  356.         // Dump constant pool item 5's bytes.
  357.  
  358.         DumpConstantPoolItem(ircf, 5);
  359.  
  360.         irmf.ClearBreakpoint(cnBreakpointPC);
  361.  
  362.         System.out.println("Cleared breakpoint.");
  363.  
  364.         throw new ComSuccessException();
  365.     }
  366.  
  367.     public void DebugStringEvent(IRemoteThread irth, String sDebugMsg)
  368.     {
  369.         System.out.println("Received IRemoteProcessCallback::DebugStringEvent().");
  370.  
  371.         throw new ComSuccessException();
  372.     }
  373.  
  374.     public void DataBreakpointEvent(IRemoteThread irth, IRemoteObject iro)
  375.     {
  376.         System.out.println("Received IRemoteProcessCallback::DataBreakpointEvent().");
  377.  
  378.         throw new ComSuccessException();
  379.     }
  380.  
  381.     public void ExceptionEvent(IRemoteThread irth, IRemoteClassField ircfException, int exceptionKind)
  382.     {
  383.         System.out.println("Received IRemoteProcessCallback::ExceptionEvent().");
  384.  
  385.         throw new ComSuccessException();
  386.     }
  387.  
  388.     public void StepEvent(IRemoteThread irth)
  389.     {
  390.         System.out.println("Received IRemoteProcessCallback::StepEvent().");
  391.  
  392.         throw new ComSuccessException();
  393.     }
  394.  
  395.     public void CanStopEvent(IRemoteThread irth)
  396.     {
  397.         System.out.println("Received IRemoteProcessCallback::CanStopEvent().");
  398.  
  399.         throw new ComSuccessException();
  400.     }
  401.  
  402.     public void BreakEvent(IRemoteThread irth)
  403.     {
  404.         System.out.println("Received IRemoteProcessCallback::BreakEvent().");
  405.  
  406.         throw new ComSuccessException();
  407.     }
  408.  
  409.     public void ThreadCreateEvent(IRemoteThread irth)
  410.     {
  411.         System.out.println("Received IRemoteProcessCallback::ThreadCreateEvent().");
  412.  
  413.         throw new ComSuccessException();
  414.     }
  415.  
  416.     public void ThreadDestroyEvent(IRemoteThread irth)
  417.     {
  418.         System.out.println("Received IRemoteProcessCallback::ThreadDestroyEvent().");
  419.  
  420.         throw new ComSuccessException();
  421.     }
  422.  
  423.     public void ThreadGroupCreateEvent(IRemoteThread irth, IRemoteThreadGroup irthg)
  424.     {
  425.         System.out.println("Received IRemoteProcessCallback::ThreadGroupCreateEvent().");
  426.  
  427.         throw new ComSuccessException();
  428.     }
  429.  
  430.     public void ThreadGroupDestroyEvent(IRemoteThread irth, IRemoteThreadGroup irthg)
  431.     {
  432.         System.out.println("Received IRemoteProcessCallback::ThreadGroupDestroyEvent().");
  433.  
  434.         throw new ComSuccessException();
  435.     }
  436.  
  437.     public void ClassUnloadEvent(IRemoteThread irth, IRemoteClassField ircfClass)
  438.     {
  439.         System.out.println("Received IRemoteProcessCallback::ClassUnloadEvent().");
  440.  
  441.         throw new ComSuccessException();
  442.     }
  443.  
  444.     public void TraceEvent(IRemoteThread irth)
  445.     {
  446.         System.out.println("Received IRemoteProcessCallback::TraceEvent().");
  447.  
  448.         throw new ComSuccessException();
  449.     }
  450.  
  451.     public void LoadCompleteEvent(IRemoteThread irth)
  452.     {
  453.         System.out.println("Received IRemoteProcessCallback::LoadCompleteEvent().");
  454.  
  455.         throw new ComSuccessException();
  456.     }
  457. };
  458.  
  459.